package com.ruoyi.test.util;

import com.alibaba.fastjson2.JSONObject;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.enums.ReadyState;
import org.java_websocket.handshake.ServerHandshake;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ResourceBundle;

//WebSocketClient连接
public class WebSocketConn {// websocket建立连接
    private static String cid;
    private static String wsServer;
    public WebSocketClient Connect() {
        try {
            //启动websocket client
            cid = "uart_" + ResourceBundle.getBundle("config").getString("icuId");
            wsServer = ResourceBundle.getBundle("config").getString("wsServer");
            System.out.println(new Throwable().getStackTrace()[0] + "，初始化 websocket client中，wsClient = " + cid + ", wsServer = " + wsServer);
            String wsUrl = wsServer + cid;
            WebSocketClient client = new WebSocketClient(new URI(wsUrl)) {
                // 打开链接
                @Override
                public void onOpen(ServerHandshake shake) {
                    System.out.println("WebSocketClient连接建立成功");
                }

                @Override
                public void onMessage(String msg) {
                    System.out.println(new Throwable().getStackTrace()[0] + "WebSocketClient收到消息：" + msg);
                }
                // 客户端发生错误,即将关闭!
                @Override
                public void onError(Exception e) {
                    //System.out.println(new Throwable().getStackTrace()[0] + "WebSocketClient发生错误已关闭：" + e);
                }

                // 关闭链接
                @Override
                public void onClose(int arg0, String arg1, boolean arg2) {
                    //System.out.println(new Throwable().getStackTrace()[0] + "WebSocketClient连接已关闭， arg1 = " + arg1);
                }
            };
            client.connect();
            return client;
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 关闭websocket连接
    public void closeWebSocket(HttpServletRequest request) {
        HttpSession session = request.getSession();
        WebSocketClient client = (WebSocketClient) session.getAttribute(" client");
        client.close();
        System.out.println(new Throwable().getStackTrace()[0] + "WebSocketClient主动关闭！");
    }

    public static void main(String[] arg0) throws Exception {
        WebSocketConn conn = new WebSocketConn();
        int i = 0;
        WebSocketClient client = null;
        do {
            System.out.println("websocket正在进行第 " + (i++ + 1) + "次" + "连接");
            client = conn.Connect();
            Thread.sleep(2000);
            if(i > 10) break;
        }while (client == null);

        System.out.println("websocket于" + i + " 次连接成功");

        if (client != null && client.getReadyState().equals(ReadyState.OPEN)) {
            JSONObject o = new JSONObject();
            o.put("cid", "laochen");
            o.put("toCid", cid);
            o.put("message", "太好了");
            System.out.println("正在发送消息：" + o);
            client.send(o.toJSONString());
            //休息一下等消息过来再关
            Thread.sleep(12000);
            client.close();
        }else {
            System.out.println("无法连接到服务器：" + wsServer);
        }
    }
}






















